package cn.com.bob.messageboard.service.impl;

import cn.com.bob.messageboard.service.ex.EmailCodeException;
import cn.com.bob.messageboard.service.ex.ServiceException;
import cn.com.bob.messageboard.service.ex.UserExistException;
import cn.com.bob.messageboard.service.ex.UserMsgException;
import cn.com.bob.messageboard.utils.JwtConstant;
import cn.com.bob.messageboard.utils.JwtTokenUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.com.bob.messageboard.domain.User;
import cn.com.bob.messageboard.service.UserService;
import cn.com.bob.messageboard.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import static cn.com.bob.messageboard.controller.VerificationCodeController.emailMap;

/**
* @author 12948
* @description 针对表【user(用户表;)】的数据库操作Service实现
* @createDate 2023-07-11 16:34:28
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{
    @Autowired
    private UserMapper userMapper;

    @Override
    public void register(User user) {
        //验证用户名是否存在
        User username = userMapper.selectOne(new QueryWrapper<User>().eq("username", user.getUsername()));
        if(username != null){
            //用户名已经存在
            throw new UserExistException("用户名已经存在");
        }
        //验证用户的邮箱
        //获取发送的验证码
        String emailCode = emailMap.get("emailCode");
        System.out.println("emailCode " + emailCode);
        //获取用户输入的验证码
        String code = user.getEmailCode();
        System.out.println("code " + code);
        if(!emailCode.equals(code)){
            //验证码未通过
            throw new EmailCodeException("验证码未通过");
        }
        //验证通过补充用户信息
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());

        //用户完成注册
        if(userMapper.insert(user) != 1){
            throw new ServiceException("服务器出现未知异常");
        }

    }

    /**
     * 用户登录
     * @param username:
     * @param password:
     * @return void
     */
    @Override
    public User login(String username, String password) {
        //首先查看用户是否存在
        User user = userMapper.selectOne(new QueryWrapper<User>().eq("username", username));
        if(user == null){
            throw new UserMsgException("用户不存在");
        }
        //检查密码是否正确
        if(!user.getPassword().equals(password)){
            throw new UserMsgException("用户密码错误");
        }

        User res = new User();
        res.setUserId(user.getUserId());
        res.setUsername(username);

        return res;


    }

    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 供微信公众号登录
     * @param username:
     * @return void
     */
    @Override
    public void login(String username, HttpServletResponse response){
        User user = userMapper.selectOne(new QueryWrapper<User>().eq("username", username));
        //创建jwt
        Map<String, Object> map = new HashMap<>();
        map.put(JwtConstant.USER_INFO_USERNAME,user.getUsername());
        map.put(JwtConstant.USER_INFO_ID,user.getUserId());
        map.put(JwtConstant.JWT_CREATE_TIME,new Date());
        String jwt = JwtTokenUtil.createToken(map);
        Cookie cookie = new Cookie("token",jwt);
        cookie.setMaxAge(60*60*24);
        cookie.setPath("/");
        response.addCookie(cookie);
        redisTemplate.opsForValue().set(user.getUserId().toString(),jwt);

    }

    /**
     * 通过微信公众号注册
     * @param username:
     * @return void
     */
    @Override
    public void register(String username) {
        User user = new User();
        user.setUsername(username);
        user.setPassword("123456");
        userMapper.insert(user);
    }
}




